/**
 * 赛季管理初始化
 */
var Season = {
    id: "SeasonTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Season.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '备注', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {
            title: '是否当前赛季', visible: true, formatter: function (value, row, index) {
            var isDefault = row.default;
            var id = row.id;
            var button = "";
            if (isDefault) {
                button += '<button type="button" class="btn btn-default" onclick="Season.setDefault(' + id + ',false)">取消</button>';
            } else {
                button += '<button type="button" class="btn btn-primary" onclick="Season.setDefault(' + id + ',true)">选择</button>';
            }
            return button;
        }
        }
    ];
};
Season.setDefault = function (id, isDefault) {
    $.ajax({
        url: Feng.ctxPath + '/season/setDefault?id=' + id + "&isDefault=" + isDefault ,
        success: function (res) {
            Feng.success("修改成功!");
            Season.table.refresh();
        },
        fail: function (res) {
            Feng.error("修改失败!" + res.responseJSON.message + "!");
        }
    })
}
/**
 * 检查是否选中
 */
Season.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Season.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加赛季
 */
Season.openAddSeason = function () {
    var index = layer.open({
        type: 2,
        title: '添加赛季',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/season/season_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看赛季详情
 */
Season.openSeasonDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '赛季详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/season/season_update/' + Season.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除赛季
 */
Season.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/season/delete", function (data) {
            Feng.success("删除成功!");
            Season.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("seasonId", this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询赛季列表
 */
Season.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Season.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Season.initColumn();
    var table = new BSTable(Season.id, "/season/list", defaultColunms);
    table.setPaginationType("client");
    Season.table = table.init();
});
